<?php
/**
 * Settings Model
 * @author 		TAMDV
 * @date		2008 08 06 (yyyy MM dd)
 * @package    	Joomus.JBS
 * @subpackage 	Components
 * @link 		http://localhost/jbschedule/administrator/index.php?option=com_jbschedule
 * @license		GNU/GPL
 */

// no direct access
//defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.application.component.model' );

/**
 * Settings Model
 *
 * @package    	Joomus.JBS
 * @subpackage	Components
 */
class JBSModelSettings extends JModel {
	/**
	 *	Default construtor
	 *
	 */
	function __construct()
	{
		parent::__construct();

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
	}
	/**
	 * Method to set the settings identifier
	 *
	 * @access	public
	 * @param	int Hello identifier
	 * @return	void
	 */
	function setId($id)
	{
		// Set id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}

	/**
	 *
	 * Get All data
	 */
	function &getAllData()
	{
		// Load all data
		if (empty( $this->_data )) {
			$query = ' SELECT * FROM #__jbschedule_settings ';
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObjectList();
		}
		if (!$this->_data) {
			$this->_data = null;
		}
		return $this->_data;
	}

	/**
	 * Method to get an Asset Groups Object
	 * @return object with data
	 */
	function &getData()
	{
		// Load the data
		if (empty( $this->_data )) {
			$query = ' SELECT * FROM #__jbschedule_settings '.
					'  WHERE id = '.$this->_id;
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0;
			$this->_data->send_notify_email = null;
			$this->_data->sender_address = null;
			$this->_data->week_start_on = null;
			$this->_data->default_calendar_page = null;
			$this->_data->time_format = null;
			$this->_data->theme = null;
		}
		return $this->_data;
	}
	/*
	 * Get The first row
	 * */
	function &getFirstData()
	{
		// Load the data
		if (empty( $this->_data )) {
			$query = ' SELECT * FROM #__jbschedule_settings LIMIT 1';
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0;
			$this->_data->send_notify_email = '1';
			$this->_data->sender_address = null;
			$this->_data->week_start_on = '0';
			$this->_data->default_calendar_page = '1';
			$this->_data->time_format = '0';
			$this->_data->theme = 'blue';
		}
		return $this->_data;
	}
    /**
	 * Method to store a record
	 *
	 * @access	public
	 * @return	boolean	True on success
	 */
	function store()
	{
		// BEGIN: Server Side Validation
		$jAp=& JFactory::getApplication();
		if ($_POST['check']!=JUtility::getToken()) {
			if ($_POST['check']=='post')
				$jAp->enqueueMessage('Please check all the fields of the form.','error');
			return false;
		}
		// END: Server Side Validation

		$row =& $this->getTable();

		$data = JRequest::get( 'post' );

		// Bind the form fields to the hello table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Make sure the record is valid
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Store the web link table to the database
		if (!$row->store()) {
			$this->setError( $row->getErrorMsg() );
			return false;
		}

		return true;
	}

}

?>